package com.chao.tlias.controller;

import com.chao.tlias.Pojo.Emp;
import com.chao.tlias.Pojo.PageBean;
import com.chao.tlias.Pojo.Result;
import com.chao.tlias.anno.Log;
import com.chao.tlias.service.EmpService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;

/**
 * 员工控制器
 */
@Slf4j
@RestController
@RequestMapping("/emps")
public class EmpController {

    @Autowired
    private EmpService empService;

    /**
     * 分页查询员工信息
     * 根据姓名，性别，入职日期进行条件查询
     * @param page
     * @param pageSize
     * @param name
     * @param gender
     * @param begin
     * @param end
     * @return
     */
    @GetMapping()
    public Result Page(@RequestParam(defaultValue = "1") Integer page,
                       @RequestParam(defaultValue = "10") Integer pageSize,
                       String name, Short gender,
                       @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate begin,
                       @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate end) {

        log.info("分页查询员工数据,{},{},{},{},{},{}", page, pageSize,name,gender,begin,end);
        PageBean pageBean = empService.page(page, pageSize,name,gender,begin,end);

        return Result.success(pageBean);
    }

    @DeleteMapping("/{id}")
    public Result deleteByID(@PathVariable Integer id) {
        log.info("根据ID删除员工：{}", id);
        empService.deleteByID1(id);
        return Result.success();
    }

    /**
     * 批量删除员工
     * @param ids
     * @return
     */
    @Log
    @DeleteMapping("/{ids}")
    public Result delete(@PathVariable("ids") Integer[] ids) {
        log.info("批量删除员工信息,{}", ids);
        empService.deleteEmps(ids);
        return Result.success();
    }

    /**
     *  新增员工
     * @param emp
     * @return
     */
    @Log
    @PostMapping()
    public Result add(@RequestBody Emp emp) {
        log.info("新增员工,{}", emp);
        empService.addEmp(emp);
        return Result.success();
    }

    /**
     * 根据ID查询员工
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public Result getByID(@PathVariable Integer id) {
        log.info("根据ID查询员工：{}", id);
        Emp emp = empService.getByID(id);
        return Result.success(emp);
    }

    /**
     * 修改员工信息
     */
    @Log
    @PutMapping
    public Result update(@RequestBody Emp emp) {
        log.info("修改员工：{}",emp);
        empService.update(emp);
        return Result.success();
    }
}
